<div id="categories_wrapper">

<h1>Kategóriák</h1>
<?php 

//$g_categories = $categories;
$rootCategories = $categories->rootCategories();

printListItems($rootCategories, $categories);
echo '<ol><li><a class="category-item" data-id="0" data-name="' . $categories->getUnsortedCategory()->name . '" >' . $categories->getUnsortedCategory()->name . '</a></li></ol>';


function printListItems($listItems, $categories) 
{
	echo "<ol>";
	$index = 0;
	$length = count($listItems);
	foreach($listItems as $item)
	{
		if($length == 1) {
			$type = 3;		
		} elseif($index == 0) {
			$type = -1;	
		} elseif($index == $length-1) {
		 	$type = 1;				
		} else {
			$type = 0;
		}
		
		echo "<li>";
	
		echoListItem($item->category_id, $item->name, $type);
		
		printListItems($categories->childrens($item), $categories);
		
		echo "</li>";
		
		$index++;
	}
	echo "</ol>";
}

function echoListItem($id, $name, $type) {
	$add  = '<span class="category-new" data-id="'.$id.'">Új</span>';
	$addInside = '<span class="category-new" data-id="'.$id.'10"> / Új </span>';
	if($type == -1 || $type == 3) $up = '';
	else $up = '<a class="up-arrow" data-id="'.$id.'"><span class="glyphicon glyphicon-arrow-up"></span></a>';
	if($type == 1 || $type == 3) $down = '';
	else $down = '<a class="down-arrow" data-id="'.$id.'"><span class="glyphicon glyphicon-arrow-down "></span></a>';
	$arrows = '<span class="category-arrows" >' . $up . $down . '</span>';
	$trash = '<a class="remove-icon" data-id="'.$id.'"><span class="glyphicon glyphicon-remove"></span></a>';
	$edit = '<a class="edit-icon" data-id="'.$id.'" data-name="'.$name.'"><span class="glyphicon glyphicon glyphicon-pencil"></span></a>';

	$listItem = $add . '<li id="'.$id.'">'.$arrows.'<a class="category-item" data-id="'.$id.'" data-name="'.$name.'"><span id="category-name_'.$id.'">'.$name.'</span></a>' . $trash . $edit . $addInside;
	
	echo $listItem;
}

?>
<button id="addCategoryButton" type="button" class="btn btn-default">Új kategória</button>
</div>

<script>
	if(!smaragdAdmin) var smaragdAdmin = {};
	if(!smaragdAdmin.categories) smaragdAdmin.categories = new Array();
	smaragdAdmin.categories = <?php echo json_encode($categories->getAllCategories() ); ?>;
</script>